<?
class parte{

	// Propiedades
	var $id;
	var $codigo_parte;
	var $descripcion;
	var $zona;
	var $tipo;
	var $genero;
	var $plural;
	var $lado;
	var $observaciones;

	var $total;

	function get($conn, $id){
		try {
			$q = "SELECT * FROM partes WHERE id=$id";
			//die($q);
			$r = $conn->Execute(utf8_encode($q));
			if(!$r->EOF){
				$this->id = $r->fields['id'];
				$this->codigo_parte = $r->fields['codigo'];
				$this->descripcion = $r->fields['descripcion'];
				$this->zona = $r->fields['zona'];
				$this->tipo = $r->fields['tipo'];
				$this->genero = $r->fields['genero'];
				$this->plural = $r->fields['plural'];
				$this->lado = $r->fields['lado'];
				$this->observaciones = $r->fields['observaciones'];

			}
		}
		catch( ADODB_Exception $e ){
			if($e->getCode()==-1)
				return ERROR_CATCH_VFK;
			elseif($e->getCode()==-5)
				return ERROR_CATCH_VUK;
			else
				return ERROR_CATCH_GENERICO;
		}
	}

	function get_all($conn, $orden="codigo", $Tipo, $Valor, $TipoE, $max=10, $from=1){
		try {
			$TipoE = empty($TipoE) ? 0 : $TipoE;
			
			if(empty($Valor)){
				$q = "SELECT id FROM partes ";
				$q.= "ORDER BY $orden ";
			}
			elseif($Tipo==0){
				$q = "SELECT id FROM partes ";
				$q.= "WHERE id::text ILIKE '%$Valor%'::text ";
				$q.= "ORDER BY $orden ";
			//	die($q);			

			}
			elseif($Tipo==1){
				$q = "SELECT id FROM partes ";
				$q.= "WHERE codigo::text ILIKE '%$Valor%'::text ";
				$q.= "ORDER BY $orden ";
			}
			elseif($Tipo==2){
				$q = "SELECT id FROM partes ";
				$q.= "WHERE descripcion::text ILIKE '%$Valor%'::text ";
				$q.= "ORDER BY $orden ";
			}

			//die($q);			
			$r = ($max!=0) ? $conn->SelectLimit($q, $max, $from) : $conn->Execute(utf8_encode($q));
			$coleccion=array();
			while(!$r->EOF){
				$ue = new parte;
				$ue->get($conn, $r->fields['id']);
				$coleccion[] = $ue;
				$r->movenext();
			}
			$this->total = $r->RecordCount();
			return $coleccion;
		}
		catch( ADODB_Exception $e ){
			if($e->getCode()==-1)
				return ERROR_CATCH_VFK;
			elseif($e->getCode()==-5)
				return ERROR_CATCH_VUK;
			else
				return ERROR_CATCH_GENERICO;
		}
	}

	function add($conn, $descripcion, $zona_c, $zona_d, $zona_t, $tipo_c, $tipo_m, $tipo_a, $tipo_o, $tipo_e, $tipo_t, $lado_i, $lado_d, $observaciones ){ 
		try {
			$zona = $zona_c . $zona_d . $zona_t;
			$tipo = $tipo_c . $tipo_m . $tipo_a . $tipo_o . $tipo_e . $tipo_t;
			$lado = $lado_i . $lado_d;
			
			$q = "INSERT INTO partes ";
			$q.= "(descripcion, zona, tipo, lado, observaciones) ";
			$q.= "VALUES ";
			$q.= "(upper('$descripcion'), NULLIF('$zona',''), NULLIF('$tipo',''), NULLIF('$lado',''), upper('$observaciones')) ";
			//die($q);
			$conn->Execute(utf8_encode($q));
			
			$this->auditoria($conn, $q);
			
			return REG_ADD_OK;
		}
		catch( ADODB_Exception $e ){
			if($e->getCode()==-1)
				return ERROR_CATCH_VFK;
			elseif($e->getCode()==-5)
				return ERROR_CATCH_VUK;
			else
				return ERROR_CATCH_GENERICO;
		}
	}

	function set($conn, $id, $codigo, $descripcion, $zona_c, $zona_d, $zona_t, $tipo_c, $tipo_m, $tipo_a, $tipo_o, $tipo_e, $tipo_t, $lado_i, $lado_d, $observaciones){
		try {
			$zona = $zona_c . $zona_d . $zona_t;
			$tipo = $tipo_c . $tipo_m . $tipo_a . $tipo_o . $tipo_e . $tipo_t;
			$lado = $lado_i . $lado_d;
			
			$q = "UPDATE partes SET codigo=upper('$codigo'), descripcion=upper('$descripcion'), zona=NULLIF('$zona',''), tipo=NULLIF('$tipo',''), lado=NULLIF('$lado',''), observaciones = upper('$observaciones') ";
			$q.= "WHERE id=$id";	
			//die($q);
			$conn->Execute(utf8_encode($q));
			
			$this->auditoria($conn, $q);
			
			return REG_SET_OK;
		}
		catch( ADODB_Exception $e ){
			if($e->getCode()==-1)
				return ERROR_CATCH_VFK;
			elseif($e->getCode()==-5)
				return ERROR_CATCH_VUK;
			else
				return ERROR_CATCH_GENERICO;
		}
	}

	function del($conn, $id){
		try {
			$q = "DELETE FROM partes WHERE id=$id";
			//die($q);
			$conn->Execute(utf8_encode($q));
			
			$this->auditoria($conn, $q);
			
			return REG_DEL_OK;
		}
		catch( ADODB_Exception $e ){
			if($e->getCode()==-1)
				return ERROR_CATCH_VFK;
			elseif($e->getCode()==-5)
				return ERROR_CATCH_VUK;
			else
				return ERROR_CATCH_GENERICO;
		}
	}

	function total_registro_busqueda($conn,$Tipo, $Valor, $TipoE, $orden="A.int_cod")
	{
		$TipoE = empty($TipoE) ? 0 : $TipoE;
		if(empty($Valor)){
			$q = "SELECT count(*) AS total FROM partes";
		}
		elseif($Tipo==0){
			$q = "SELECT count(*) AS total FROM partes";
			$q.= " WHERE id::text ILIKE '%$Valor%'::text";
		}
		elseif($Tipo==1){
			$q = "SELECT count(*) AS total FROM partes";
			$q.= " WHERE codigo::text ILIKE '%$Valor%'::text";
		}
		elseif($Tipo==2){
			$q = "SELECT count(*) AS total FROM partes";
			$q.= " WHERE descripcion::text ILIKE '%$Valor%'::text";
		}

		$r = $conn->Execute(utf8_encode($q));
		$total = $r->fields['total'];
		return $total;
	}

	function auditoria($conn, $query) {
		$id_usuario = $_SESSION['id'];
		$q = "INSERT INTO auditoria(id_usuario, query) VALUES($id_usuario,$$" . $query . "$$);";
		//die($q);
		$conn->Execute(utf8_encode($q));

		return true;
	}		
	
}
?>
